package com.chat.view;

import com.chat.Local;
import com.chat.service.*;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.util.Objects;

/*
 群成员界面的Controller
 */
public class GroupMemberController {
    public ListView groupmember;
    public static ObservableList<MemberData> member= FXCollections.observableArrayList();
    public Button setbutton;
    public Button cancelbutton;
    public String select;//当前选中的群成员id
    public Button transferbutton;

    public void transfer(){
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
                alert.setHeaderText("恭喜");
                alert.setContentText("群主转让成功！");
                alert.showAndWait();
            }
        });//给用户的提示
        SetState ss=new SetState();
        ss.transfer(Local.group.getId(),select);
        ss.cancel(Local.group.getId(),Local.group.getGroupleader());
        Local.group.setGroupleader(select);
        initialize();
    }//群主转让

     public void start(){
         setbutton.setVisible(false);
         cancelbutton.setVisible(false);
         transferbutton.setVisible(false);
         groupmember.getItems().clear();
         member.clear();
         GetGroupMember ggm=new GetGroupMember();
         ggm.get(Local.receive);
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         if (Local.user.getUsername().equals(Local.group.getGroupleader())){
             setbutton.setVisible(true);
             cancelbutton.setVisible(true);
             transferbutton.setVisible(true);
             setbutton.setDisable(true);
             transferbutton.setDisable(true);
             cancelbutton.setDisable(true);
         }
     }
    public void initialize(){
         start();
        groupmember.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);//设成单选

        groupmember.setItems(member);

        groupmember.setCellFactory(new Callback<ListView, ListCell>() {
            @Override
            public ListCell call(ListView param) {
                return new ListCell<MemberData>(){
                    @Override
                    protected void updateItem(MemberData item, boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hbox=new HBox();
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("memberlist.fxml"));
                            try {
                                hbox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            setOnMouseClicked(event ->{
                                select=item.username;
                                   if(Objects.equals(item.state, "2")){
                                       cancelbutton.setDisable(false);
                                       setbutton.setDisable(true);
                                       transferbutton.setDisable(false);
                                   }
                                   else if(Objects.equals(item.username, Local.group.getGroupleader())){
                                        setbutton.setDisable(true);
                                        cancelbutton.setDisable(true);
                                        transferbutton.setDisable(true);
                                   }
                                   else{
                                       transferbutton.setDisable(false);
                                       setbutton.setDisable(false);
                                       cancelbutton.setDisable(true);
                                   }
                                    });
                            MemberListController memberListController=fxmlLoader.getController();
                            hbox.setPadding(new Insets(20,0,0,0));
                            hbox.setPrefHeight(50);
                            memberListController.setNickname(item.nickname);
                            memberListController.setState(item.state);
                            memberListController.setTouxiang(item.username);
                            Platform.runLater(()-> {
                                this.setGraphic(hbox);
                            });
                        }
                        else {
                            Platform.runLater(()-> {
                                this.setGraphic(null);
                            });

                        }
                    }
                };
            }
        });

    }

    public void canceladmin(MouseEvent mouseEvent) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
                alert.setHeaderText("恭喜");
                alert.setContentText("成功将其设为管理员！");
                alert.showAndWait();
            }
        });//给用户的提示
        SetState ca=new SetState();
        ca.cancel(Local.group.getId(),select);
        start();
    }

    public void setadmin(){
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
                alert.setHeaderText("恭喜");
                alert.setContentText("成功将其设为管理员！");
                alert.showAndWait();
            }
        });//给用户的提示
        SetState sa=new SetState();
        sa.set(Local.group.getId(),select);
       start();
    }//设置管理员的点击事件

    public void tuichu(){
        Stage stage=(Stage) groupmember.getScene().getWindow();
        stage.close();
        try {
           GroupMsgView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
